<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp"%>
<%@ include file="/WEB-INF/pages/common/back-common.jsp"%>
<%@ include file="/WEB-INF/pages/common/jquery2.1.4.jsp"%>
<%@ include file="/WEB-INF/pages/common/back-dialog.jsp"%>
<html>
<head>
<title>创建</title>
<script src="${contextPath}/resources/common/js/jquery.validate.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" media="screen" href="${contextPath}/resources/common/css/errorform.css" />
<script src="<ls:templateResource item='/resources/plugins/My97DatePicker/WdatePicker.js'/>" type="text/javascript"></script>
<link rel="stylesheet" href="<ls:templateResource item='/resources/plugins/kindeditor/themes/default/default.css'/>" />
<link rel="stylesheet" href="<ls:templateResource item='/resources/plugins/kindeditor/plugins/code/prettify.css'/>" />
<script charset="utf-8" src="<ls:templateResource item='/resources/plugins/kindeditor/kindeditor-min.js'/>"></script>
<script charset="utf-8" src="<ls:templateResource item='/resources/plugins/kindeditor/lang/zh_CN.js'/>"></script>
<script charset="utf-8" src="<ls:templateResource item='/resources/plugins/kindeditor/plugins/code/prettify.js'/>"></script>
<link href="<ls:templateResource item='/resources/plugins/select2-4.0/select2.css'/>" rel="stylesheet" />
<script src="<ls:templateResource item='/resources/plugins/select2-4.0/select2.full.min.js'/>" type="text/javascript"></script>
<script src="<ls:templateResource item='/resources/plugins/select2-4.0/i18n/zh-CN.js'/>" type="text/javascript"></script>
<script src="<ls:templateResource item='/resources/plugins/select2-4.0/makeSelect2.js'/>" type="text/javascript"></script>
<script type="text/javascript" src="<ls:templateResource item='/resources/common/js/infinite-linkage.js'/>"></script>
<style>
.dailyLog table th, .dailyLog table td {
	padding: 10px 5px !important;
}

.dailyLog table td input {
	height: 28px;
	text-indent: 5px;
	outline: none;
}
</style>
</head>
<body>
	<form:form  action="${contextPath}/admin/task/loadTaskEstimateByDay/${taskDate}" id="form2" method="post">
		<input type="hidden" id="curPageNO" name="curPageNO" value="${curPageNO}" />
		<input type="hidden" id="userName" name="userName" value="${userName}" />
		
		<!-- 已经做过的目标列表 -->
	    <div id="taskEstimatePage"></div>
	</form:form>
 
	<form method="post" id="form1">
		<input id="id" name="id" value="${dailyLog.id}" type="hidden">
		<div align="center" class="dailyLog">
			<table border="0" align="center" class="${tableclass} " id="col1" style="padding: 5px; width: 100%">
				<thead>
					<tr class="sortable">
						<th style="width: 30px; text-align: center;">ID</th>
						<th>项目</th>
						<th><font color="red">启动</font>状态的目标</th>
						<th>工时</th>
						<th style="min-width: 250px">日志</th>
					</tr>
				</thead>
				<c:forEach begin="0" end="3" varStatus="index">
					<tr class="data">
						<td style="width: 30px; text-align: center;">${index.count}</td>
						<td>
							<select name="projectId" id="projectId" class="projectId" style="width: 300px">
						   </select>
						</td>
						<td>
						    <select name="taskList" id="taskList" class="taskList" style="width: 300px">
						    	<option value="">-- 请选择目标 --</option>
						   </select>
						   <a id="newTask" style="display:none;" target="_blank">新建目标</a>
						   <a id="taskPlan" style="display:none;" target="_blank">里程碑</a>
						</td>
						<td><input type="text" name="taskestimate" style="width: 50px;" /></td>
						<td><input type="text" name="remark" style="width: 400px;" /></td>
					</tr>
				</c:forEach>
				<tr>
					<td colspan="7">
						<div align="center">
							<input type="button" name="Submit" id="Submit" value="提交" />
						</div>
					</td>
				</tr>
			</table>
			<div style="text-align: left">
			    填写说明：<br/>
			    1. 选择日志对应的日期<br/>
				2. 填写日志之前需要填写对应的项目，<span style="color: red;">如果没有找到相关的项目，请联系管理员增加项目</span><br/>
				3. 目标不是必填项，可以点击"新建"按钮,为对应的项目添加目标<br/>
				4. 需要填写每个日志所消耗的时间
			</div>
		</div>
	</form>
	<script language="javascript">
		    $.validator.setDefaults({
		        submitHandler: function() {
		            alert("提交事件!");
		          }
		    });

    $(document).ready(function() {
    	var contextPath = '${contextPath}';
    	$("select.combox").initSelect();
    	makeSelect2(contextPath + "/admin/project/common/loadMyProjectEntities",".projectId","项目","value","key");
    	$(".taskList").select2();
    	
    	//更改目标列表
    	$('.projectId').change(function() {
    		var projectId = $(this).val();
			 var $this = $(this);
			var task =  $this.parent().parent().find("select[name='taskList']");
			var newTask =  $this.parent().parent().find("a[id='newTask']");
			var taskPlan =  $this.parent().parent().find("a[id='taskPlan']");


			newTask.attr("href", contextPath + "/admin/task/loadByProject/" + projectId);
			taskPlan.attr("href", contextPath + "/admin/projectPlan/query/" + projectId);
			if(projectId != null){
				newTask.show();
				taskPlan.show();
			}else{
				newTask.hidden();
				taskPlan.hidden();
			}
			
			
			task.empty();
			task.append('<option value="">-- 请选择目标 --</option>');
		  	   $.ajax({
		           type: 'post', // 提交方式 get/post
		           url:  "${contextPath}/admin/task/taskList", // 需要提交的 url
		           data: {"projectId": projectId},
		           dataType : 'json', 
		           async: false,//默认为true 异步  
		           error: function(jqXHR, textStatus, errorThrown) {
		    	 		 layer.alert("查询失败" + textStatus);
		    		},
		           success: function(data) { // data 保存提交后返回的数据，一般为 json 数据
		               // 此处可对 data 作相关处理
		               //console.log(data)
		               if(data.total > 0){
		            	   $.each(data.results, function(idx, obj) {
		            		   // console.log(obj.id + "," + obj.text);
		            		    task.append('<option value="' + obj.id + '">' + obj.text + '</option>');
		            		});
		               }
		           }
		       });
			 
		});
    	
    	
    	
    	//加载日志
    	sendData();
	 //binding Submit
  	 $("#Submit").click(function(){
  		    var formData = [];
  		    var isBreak = false;
	  	    $(".data").each(function(){
	  	    var $this = $(this);
	  	    var projectId = $.trim($this.find("select[name='projectId']").val());
	  	    var taskId = $.trim($this.find("select[name='taskList']").val());
	  	  	var taskestimate = $.trim($this.find("input[name='taskestimate']").val());
	  		var remark = $.trim($this.find("input[name='remark']").val());
	 		if(projectId && taskestimate && taskestimate > 0 && remark){
		  		var obj = {"taskId":taskId, "taskestimate": taskestimate, "projectId":projectId, "remark": remark};
		  		formData.push(obj);
	 		}
	 		
	  	  });
	  	  
	  	  if(!isBreak && formData.length == 0){
	  			layer.msg("数据有误:工时必须要大于0; 每一行请填写完整.");
	  			return true; 
	  	  }
	  	
	  	var data = JSON.stringify(formData);
  	 if(!isBreak){
  	 	$("#Submit").attr("disabled","disabled");
  	 	var formData = $("#form1").serialize();
  	    $.ajax({
         type: 'post', // 提交方式 get/post
         url:  "${contextPath}/admin/task/saveTaskEstimateByDay/${taskDate}", // 需要提交的 url
         data: {"taskEstimateDto": data},
         dataType : 'json', 
         error: function(jqXHR, textStatus, errorThrown) {
  	 		 layer.alert("保存失败:必须选中目标;并且工时要大于0.");
  		},
         success: function(data) { // data 保存提交后返回的数据，一般为 json 数据
             // 此处可对 data 作相关处理
             if("success" == data){
               layer.msg("保存成功");
               closeDialog();
             }else if("fail" == data){
           	   layer.msg("保存失败:必须选中目标;并且工时要大于0.");
			 }if("noIn" == data){
				 layer.msg("目前不在项目里");
             }if("statusError" === data){
           	  layer.msg("目标状态必须要是启动或者延误状态");
             }else{
				 console.log("11111111" + data);
           	  layer.msg("保存失败:必须选中目标;并且工时要大于0.");
             }
           
         }
     });
     return false; // 阻止表单自动提交事件
  	}; 
  	});
});
    
    
    //加载日志
	function sendData(){
		var formData = $("#form2").serialize();
	     $.ajax({
	    		url:"${contextPath}/admin/task/loadTaskEstimateByDay/${taskDate}", 
	    		type:'post', 
	    		data:formData,
	    		async : true, //默认为true 异步   
	    		error: function(jqXHR, textStatus, errorThrown) {
	    		 //  alert("系统异常" + " jqXHR= " + jqXHR + ", textStatus = " + textStatus + ",errorThrown =  " + errorThrown);
	    		},
	    		success:function(result){
	    		   $("#taskEstimatePage").html(result);
	    		}
	    	});
    }
    
    function pager(curPageNO){
        document.getElementById("curPageNO").value=curPageNO;
        sendData();
    }
    
	function closeDialog(){
		var index = parent.layer.getFrameIndex("taskEstimate"); //先得到当前iframe层的索引
		parent.layer.close(index); //再执行关闭   
		if(parent.sendData){
			parent.sendData();//异步
		}else{
			//parent.location.reload();
		}
	}
	
	function deleteTaskEstimate(id, taskId){
	  	   $.ajax({
	           type: 'post', // 提交方式 get/post
	           url:  "${contextPath}/admin/task/deleteTaskEstimate/" + id, // 需要提交的 url
	           dataType : 'json', 
	           async: false,//默认为true 异步  
	           error: function(jqXHR, textStatus, errorThrown) {
	    	 		 layer.alert("删除失败" + textStatus);
	    		},
	           success: function(data) { // data 保存提交后返回的数据，一般为 json 数据
	               // 此处可对 data 作相关处理
	               if("success" == data){
	                 layer.msg("删除成功");
	                 window.location.reload();
	                 parent.sendData();
	               }else{
	             	 layer.msg("删除失败");
	               }
	             
	           }
	       });
	   
	}
</script>
</body>
</html>
